#include "Guard.h"

Guard::Guard (sf::Vector2f position,bool flip, Artman *artman,AnimationSolver *animator)
{
	m_position=position;
	m_velocity=sf::Vector2f(0,0);
	m_state=CS_fall;
	p_animator=animator;
	this->setPosition(position);
	m_flip=flip;
	this->setTexture(*artman->getTexture("guard.png"));
	this->setOrigin(32,32);
	m_frame=0;
	m_frameclock=0;
	this->setTextureRect(animator->getRect(2,m_state,&m_frame,m_flip));
	m_miniview=artman->getTexture("miniviewbasic.png");
	m_type=AT_enemy;
	m_collision=CE_collide;
	m_alerted=false;
	m_ticker=0;
}

void Guard::HandleCV(float dt)
{
	m_ticker+=dt;
	if (m_ticker>=8)
	{
		//remove us from existence
		std::vector <Actor*> *actorlist=p_solver->getActorvector();
		for (int i=0;i<actorlist->size();i++)
		{
			if (actorlist->operator[](i)==this)
			{
				actorlist->erase(actorlist->begin()+i);
			}
		}
		delete this;
	}
}

void Guard::Update(float dt)
{
	m_frameclock+=dt;

	if (m_frameclock>1)
	{
		this->setTextureRect(p_animator->getRect(0,m_state,&m_frame,m_flip));
		m_frameclock=0;
	}
	if (NotShlurped())
	{
		Gravity(dt);

		Collision(dt);

		m_position+=(m_velocity*dt);
		this->setPosition(m_position);
	}
	else
	{
		HandleCV(dt);
	}
}

void Guard::ExtraDraw(sf::RenderWindow &window)
{



}

bool Guard::Vore(bool flip)
{
	if (!m_alerted || m_state!=CS_defeat)
	{
		if (m_state!=CS_defeat)
		{
			m_state=CS_stealthcv;
		}
		else
		{
			m_state=CS_defeatcv;
		}
		m_flip=flip;
		return true;
	}
	return false;
}